﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ActivityTracker.Library;
using System.Collections;
using Common.Library;


namespace ActivityTracker
{
    public partial class AddNewSchedule : PhoneApplicationPage
    {
        #region Global

        Actvity objActivity = new Actvity();
        Schedules objSchedules = new Schedules();
        string actId = string.Empty;
        bool newPageInstance = false;

        #endregion

        #region Constructor

        public AddNewSchedule()
        {
            InitializeComponent();
            newPageInstance = true;
        }

        #endregion

        #region Events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (NavigationContext.QueryString.TryGetValue("actId", out actId))
                    App.actId = actId;
                if (App.actId != null) txtblkactivity.Text = objActivity.GetActivityName(App.actId);
                if (App.schedName != null) tblkScheduleType.Text = App.schedName;
                if (newPageInstance)
                {
                    if (App.schedStartDate != null) DPstart.Value = App.schedStartDate;
                    if (App.schedStartTime != null) Dpstarttime.Value = App.schedStartTime;
                    if (App.schedEndDate != null) DPend.Value = App.schedEndDate;
                    if (App.schedEndTime != null) Dpendtime.Value = App.schedEndTime;
                }
                newPageInstance = false;
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In AddNewSchedule file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (tblkScheduleType.Text != "")
                {
                    tblkScheduleValidation.Visibility = Visibility.Collapsed;
                    objSchedules.SaveNewSchedule((DateTime)DPstart.Value, (DateTime)Dpstarttime.Value, (DateTime)DPend.Value, (DateTime)Dpendtime.Value, tblkScheduleType.Text, App.actId, txtblkactivity.Text, App.schedId);
                    App.actId = ""; App.schedId = ""; App.schedName = ""; App.schedStartDate = DateTime.Now; App.schedStartTime = DateTime.Now;
                    App.schedEndDate = DateTime.Now; App.schedEndTime = DateTime.Now;
                    NavigationService.Navigate(new Uri("/Views/Activities/MyActivities.xaml", UriKind.Relative));
                }
                else
                    tblkScheduleValidation.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarSave_Click Method In AddNewSchedule file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarHelp_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Help/Help.xaml?id=6&url=tTXx-PWncn8", UriKind.Relative));
           
        }

        private void tblkSchedType_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.schedStartDate = (DateTime)DPstart.Value;
            App.schedStartTime = (DateTime)Dpstarttime.Value;
            App.schedEndDate = (DateTime)DPend.Value;
            App.schedEndTime = (DateTime)Dpendtime.Value;
            NavigationService.Navigate(new Uri("/Views/Schedules/ScheduleTypes.xaml", UriKind.Relative));
        }

        #endregion
    }
}